package com.jtjrenren.android.taxi.passenger.core.order.disptach;

import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.bean.Order;
import com.jtjrenren.android.taxi.passenger.bean.protocl.CommonResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderAcceptResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderDispatchResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderFilterResponse;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.manger.push.cmd.ControlCmd;
import com.jtjrenren.android.taxi.passenger.manger.push.cmd.Protocl;
import com.library.mvp.presenter.BasePresenter;
import com.library.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Date;

/**
 * Created by wdl on 2016/9/20.
 */

public class PreStatusPresenterImpl extends BasePresenter<PreStatusView, PreStatusInteractor> implements PreStatusPresenter {
    private String orderserno;
    private Order order;
    private int accepDriverNum = 0;

    public PreStatusPresenterImpl(PreStatusView view) {
        super(view);
    }

    @Override
    public void init() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void destory() {
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initOrder(Order order) {
        this.order = order;
        rsendOrder();
    }

    @Override
    public void initOrderID(String orderID) {
        this.orderserno = orderID;
    }

    @Override
    public void rsendOrder() {
        view.showOrderDispatchView();
        view.startScan();

        orderserno = null;
        order.setCreateDate(new Date());
        String uploadOrderCmd = ControlCmd.getUploadOrderCmd(order);
        AppManger.getInstance().sendMsgToControl(uploadOrderCmd);
    }

    @Override
    public void cancleOrder() {
        if (StringUtils.isEmpty(orderserno)) {
            view.showTip("数据异常");
            view.finshAty();
            return;
        }
        String cancleOrderCmd = ControlCmd.getCancleOrderCmd(orderserno, 0);
        AppManger.getInstance().sendMsgToControl(cancleOrderCmd);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handOrderAcceptEvent(OrderAcceptResponse orderAcceptResponse){
        if (orderserno != null && orderserno.equals(orderAcceptResponse.getOrderSerialNo())) {
            if (StringUtils.isEmpty(orderAcceptResponse.getDriverInfo())) {
                view.showTip(AppManger.getInstance().getString(R.string.meiyousijijiedan));
                view.stopScan();
                view.finshAty();
                return;
            }
            view.jdFinish(orderserno);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handOrderFilterEvent(OrderFilterResponse orderFilterResponse){
        if (orderserno != null && orderserno.equals(orderFilterResponse.getOrderSeriNo()))
            view.showOrderFilterView(accepDriverNum);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handDispatchEvent(OrderDispatchResponse response){
        if (orderserno.equals(response.getOrderSerilNo())) {
            view.refreshNotifCar(response.getDispatchDriverNum());
            view.refreshAcceptCar(response.getNotifyDriverNum());
            accepDriverNum = response.getNotifyDriverNum();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handCommonEvent(CommonResponse commonResponse){
        if (orderserno == null && Protocl.MSG_ID_UPORDER.equals(commonResponse.getCmdId())) {
            orderserno = commonResponse.getOrderSerialID();
        } else if (Protocl.MSG_ID_ORDERCANCLE.equals(commonResponse.getCmdId()) && orderserno.equals(commonResponse.getOrderSerialID())) {
            //取消订单的通用回复,这里返回的都是0 理应将电召单号返回
            if (commonResponse.getResult().equals("0") || commonResponse.getResult().equals("00")) {
                view.dissWaitting();
                view.finshAty();
            }
        }
    }

    @Override
    public PreStatusInteractor createInteractor() {
        return null;
    }
}
